package de.gameduell.framework.util {
	
	import de.gameduell.framework.debug.Debug;	

	/**
	 * Provides access to stored FlashVars which were set via HTML or at run-time.
	 * With FlashVars, instead of passing variables after the filename,
	 * variables are passed in a separate param tag as well as in the embed tag.
	 */
	public class FlashVars {
		
		public static const APP_URL:String         = "appurl";
		public static const TESTMODE:String        = "testmode";
		public static const GAME_ID:String 	       = "gameid";
		public static const XML_FOLDER:String      = "xmlfolder";
		public static const LANGUAGE:String        = "language";
		public static const NO_VALUE:String        = "null";
		public static const LOADING_TIME:String    = "loadingtime";
		public static const ENCRYPTED_SCORE:String = "encryptedscore";
		
		private static var flashVars:Object;
		
		/**
		 * Stores all FlashVars which were set via HTML in an array.
		 * Some warnings will appear if no language or no xmlfolder was set.
		 * In this cases, the application will not work properly.
		 * 
		 * This method should be called only once. Typically this is already done
		 * by the ApplicationLoader's init()-method.
		 * 
		 * @see ApplicationLoader
		 * 
		 * @param aFlashVars	contains a set of FlashVars
		 */
		public static function catchVars(aFlashVars:Object):void {
			flashVars = aFlashVars;
			
			if(getKeyValue(XML_FOLDER) == NO_VALUE){
				Debug.trace("FlashVars::catchVars: no xmlfolder was set", Debug.WARNING);
			}
			if(getKeyValue(LANGUAGE) == NO_VALUE){
				Debug.trace("FlashVars::catchVars: no language was set", Debug.WARNING);
			}
		}

		/**
		 * Getter for key value pairs.
		 * If a key isn't available "null" will be returned.
		 * 
		 * @param key		key for the value
		 */
		public static function getKeyValue(key:String):String {
			try{
				if(flashVars[key] == null){
					return NO_VALUE;
				}else{
					return String(flashVars[key]);
				}			
			}catch(e:Error){
				Debug.trace("FlashVars::getKeyValue: key=" + key + " -> " + e.getStackTrace(),Debug.ERROR);
			}
			return NO_VALUE;
		}
		
		/**
		 * Set or overwrite a key value pair which is stored in flashVars.
		 * 
		 * @param key		key for a value
		 * @param value 	which will be set
		 */
		public static function setKeyValue(key:String, value:String):void {
			flashVars[key] = value;
		}
	}
}